
$('.welcome').find('.progress .progress-an').animate({width:'100%'}, 5000);
var windowheight = function() {
	return $(window).height()
};
var windowwidth = function() {
	return $(window).width()
};

var max760 = 760;

var mousewheelindex = 0;


$(window).on('resize', function(event) {
	$('.container').height(windowheight());
	$('.container').find('.section').height(windowheight());

	$('.home').find('.home-left').css({
		width: windowwidth() / 2,
		height: windowheight()
	});
	if (windowwidth() <= max760) {
		$('.home').find('.home-right').css({
			width: windowwidth(),
			height: windowheight()
		});
	} else {
		$('.home').find('.home-right').css({
			width: windowwidth() / 2,
			height: windowheight()
		});
	}

}).resize();


// 缓冲页
window.onload = function() {

	$('.welcome').addClass('hidden');

	
	var windowurl=window.location.href;

	var sectionid=0;

	if(windowurl.indexOf('#')<0)
	{
		sectionid=0;
	}else{
		sectionid=windowurl.substring(windowurl.indexOf('#')+1,windowurl.length);
	}

	var mousewheelswiper = new Swiper('.container', {
		paginationClickable: true,
		mode: 'vertical',
		mousewheelControl: true,
		slideActiveClass: 'active',
		onSlideChangeStart: function(swiper) {
			mousewheelindex = swiper.activeIndex;
			scrollanimate(swiper);

			$(".header").find('.nav .list li').each(function(index, el) {
				if (parseInt($(this).attr('data-index')) == swiper.activeIndex) {

					$(this).addClass('on').siblings().removeClass('on');
				}
			});
		}
	})

	mousewheelswiper.swipeTo(parseInt(sectionid));

	$(".tonext").on('click', function(event) {
		mousewheelswiper.swipeNext();
	});

	function scrollanimate(swiper) {

		switch (swiper.activeIndex) {

			case 4:
				break;
			case 7:
				if (windowwidth() <= 760) return;
				$(".header .logo").addClass('active');
				$('.qiao-contact').addClass('effect');
				break;
			case 8:
				if (windowwidth() <= 760) return;
				$(".header .logo").addClass('active');
				$('.qiao-contact').removeClass('effect');
				break;
			default:
				workswitch = true;
				if (windowwidth() <= 760) return;
				$(".header .logo").removeClass('active');
				$('.qiao-contact').removeClass('effect');
		}
	}

	// 模块一
	// =============================================================================================
	var homeSwiperright = new Swiper('.home .home-right', {
		loop: true,
		mode: 'vertical',
		onlyExternal: true
	});
	var homeSwiperleft = new Swiper('.home .home-left', {
		loop: true,
		mode: 'vertical',
		onlyExternal: true
	})

	var homenavindex = 0;
	var homenav = $(".home .home-nav");
	setInterval(function() {

			homeSwiperright.swipePrev();
			homeSwiperleft.swipeNext();
			homenav.children().eq(homenavindex).addClass('on').siblings().removeClass('on');

			homenavindex++;
			if (homenavindex == homenav.children().length) {
				homenavindex = 0;
			};

		}, 5000);

	homenav.children().on('click', function(event) {
		$(this).addClass('on').siblings().removeClass('on');
		homeSwiperright.swipeTo($(this).index());
		homeSwiperleft.swipeTo($(this).index());
	});


	// =============================================================================================
	// 模块2 服务
	var servitems = new Swiper('.servitems', {
		slidesPerView: 2,
		loop: true, //可选选项，开启循环
		onFirstInit: function(swiper) {
			$(swiper.container).find('.prev').on('click', function(event) {
				event.preventDefault();
				swiper.swipePrev();
			});
			$(swiper.container).find('.next').on('click', function(event) {
				event.preventDefault();
				swiper.swipeNext();
			});
		}
	});


	// 模块3 案例

	var caseitems = new Swiper('.caseitems', {
		slidesPerView: 'auto',
		paginationClickable: true,
		onFirstInit: function(swiper) {
			var scrollbar = $(swiper.container).next(".scrollbar");

			for (var i = 0; i < swiper.slides.length; i++) {
				scrollbar.find('.pagenum').append('<span class="bullets"></span>')
			};

			scrollbar.find('.bullets').width(scrollbar.width() / swiper.slides.length);


			var offon = false;
			var btnmg = 0;

			scrollbar.find('.btn_scroll').on('mousedown', function(event) {
				event.preventDefault();
				event.stopPropagation();
				btnmg = event.clientX - $(this).offset().left;
				if (offon) {
					offon = false;
				} else {
					offon = true;
				}

			});

			var left = 0;

			$(document).on('mousemove', function(event) {
				if (offon) {
					left = event.clientX - scrollbar.offset().left - btnmg;
					left = left < 0 ? 0 : left;
					left = left > scrollbar.width() - scrollbar.find('.btn_scroll').width() ? scrollbar.width() - scrollbar.find('.btn_scroll').width() : left;
					scrollbar.find('.btn_scroll').stop().css('left', left);
				}
			});

			$(document).on('mouseup', function(event) {
				event.preventDefault();
				if (offon) {
					offon = false;
					var navwidth = scrollbar.find('.pagenum').children().eq(0).width();
					var activeindex = parseInt(left / navwidth);
					swiper.swipeTo(activeindex);
				}
			});

		},
		onSlideChangeStart: function(swiper) {
			var scrollbar = $(swiper.container).next(".scrollbar");
			var activenav = scrollbar.find('.pagenum').children().eq(swiper.activeIndex);
			var positionleft = activenav.offset().left - scrollbar.offset().left;

			if (swiper.activeIndex == (scrollbar.find('.pagenum').children().length - 1)) {
				positionleft = activenav.position().left + scrollbar.find('.pagenum').children().width() - scrollbar.find('.btn_scroll').width();
			}
			scrollbar.find('.btn_scroll').stop(true).animate({
				left: positionleft
			}, 400);
		}
	});



	// 模块5 客户
	var custitems = new Swiper('.response .swiper-container', {
		slidesPerView: 'auto',
		onFirstInit: function(swiper) {
			$('.itemwrap').find('.next').on('click', function(event) {
				event.preventDefault();

				var caseid = parseInt($(swiper.activeSlide()).find('.item').attr("data-caseid"));

				$.ajax({
						url: './dependdetail.php._ajax.php',
						type: 'post',
						async: false,
						data: {
							caseid: caseid
						}
					})
					.done(function(data) {
						data = eval(data);
						if (data == null)
							{
							alert("编辑部未上传完毕，咨询一下 010-80445052");
							return;
							}

						var itemhtml = "";
						$.each(data, function(index, val) {

							itemhtml += '<div class="item" data-caseid="' + val.id + '">' +
								'<a href="javascript:void(0)" class="pic">' +
								'<img src="' + val.al_img + '" alt=""></a>' +
								'<dl class="intro">' +
										'<dt>' + val.al_name + '</dt>' +
										'<dd>' + val.al_abstract + '</dd>' +
										'<dt>' + val.al_url + '</dt>' +
								'</dl>' +
								'</div>';

						});
						swiper.appendSlide(itemhtml, 'swiper-slide', 'li');
						swiper.swipeNext();
					})
				
			});
		}
	});

	var NewPage = 1;
	var newsoffon= true;
	var newslist = new Swiper('.newslist .swiper-container', {
		loop: false,
		slidesPerView: 1,
		onFirstInit: function(swiper) {
			// 绑定翻页按钮

			$('.newslist').find('.next').on('click', function(event) {
				event.preventDefault();
				if (!newsoffon) {
					//alert("编辑部未上传完毕，咨询一下 010-80445052");
					swiper.swipeNext();
					return;
				};
				$.ajax({
						url: './newList_Ajax.php',
						type: 'post',
						async: false,
						data: {
							NewPage: NewPage
						}
					})
					.done(function(data) {

						data = eval(data);
						if (data == null)
						{
							newsoffon=false;
							return;
						}

						var itemhtml = "";
						$.each(data, function(index, val) {

							itemhtml += '<li data-newsid="' + val.id + '">' + val.a_title + '</li>';

						});

						itemhtml = "<ul>" + itemhtml + "</ul>";

						swiper.prependSlide(itemhtml, 'swiper-slide', 'div');

						NewPage++;

					})
					swiper.swipeNext();

				//#######################################################################################				
			});


			$('.newslist').find('.prev').on('click', function(event) {

				event.preventDefault();
				swiper.swipePrev();
			});

			//绑定点击事件
			$(swiper.slides).find('ul').find('li').on('click', function(event) {
				event.preventDefault();
				var newsid=$(this).attr('data-newsid');

				$(".n-main").find('.newsdetail').eq(0).animate({
						opacity: 0,
						top: '-100%'
					},
					1000,
					function() {
						$(".n-main").append(ajax_getcase(newsid));
						$(this).remove();
					});

			});

		}
	});


	function ajax_getcase(id) {
		var itemhtml = "";
		$.ajax({
				url: './newdetail_Ajax.php',
				type: 'post',
				async: false,
				data: {
					id: id
				}
			})
			.done(function(data) {

				data = eval(data);
				if (data == null)
				{
					//alert("已经到最后了编辑部未上传完毕，咨询一下 010-80445052");
					return;
				}

				$.each(data, function(index, val) {
					itemhtml += '<div class="newsdetail">'+
									'<div class="hd">'+
										'<h1 class="title">'+val.a_title+'</h1>'+
										'<div class="title-bar">'+val.a_time+'&nbsp;&nbsp;发布：'+val.a_uid+'</div>'+
									'</div>'+
									'<div class="bd">'+
										'<div class="content">'+val.a_content+'</div>'+
									'</div>'+
								'</div>';
				});

				
			})
			.fail(function() {
				console.log("error");
			})
			return itemhtml;
	}
	// 绑定头部导航按钮关闭打开事件
	$(".header").find('.btn_control').on('click', function(event) {
		var next = $(this).next(".list");
		if ($(this).hasClass('close')) {
			$(this).closest('.nav').animate({
				right: 0 - next.width()
			}, 300);
			$(this).removeClass('close').addClass('open');
		} else {
			$(this).closest('.nav').animate({
				right: 0
			}, 300);
			$(this).removeClass('open').addClass('close');
		}
	}).click();

	setTimeout(function(){
		$(".header").find('.btn_control').click();
	},6000);


	$(".header").find('.list li').on('click', function(event) {
		mousewheelswiper.swipeTo(parseInt($(this).attr('data-index')));
	});

	$(window).on('resize', function(event) {

		if (windowwidth() <= max760) {
			servitems.params.slidesPerView = 1;
			caseitems.params.slidesPerView = 1;
		} else {

			servitems.params.slidesPerView = 2;
			caseitems.params.slidesPerView = "auto";
		}
		servitems.reInit();
		caseitems.reInit();

		if(windowheight()<=820){
			$('.customer').find('.cooperation').hide();
			$('.customer').find('.itemlist').css('position', 'static');
			
		}else{
			$('.customer').find('.cooperation').show();
			$('.customer').find('.itemlist').css('position', 'relative');
		}
	}).resize();



	
	$('.news').find('.newsdetail').find('.bd').on('mousewheel', function(event) {

		event.stopPropagation();
	});
		

}